package org.jprogger.myworld.sample.client.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jprogger.myworld.api.exception.ApiException;
import org.jprogger.myworld.api.model.User;
import org.jprogger.myworld.api.request.ApiRequest;
import org.jprogger.myworld.api.request.ApiRequestBuilder;
import org.jprogger.myworld.api.request.DefaultApiRequestBuilder;
import org.jprogger.myworld.api.request.HttpException;
import org.jprogger.myworld.api.response.ParseResponseException;
import org.jprogger.myworld.api.secure.ApiRequestSigner;
import org.jprogger.myworld.api.secure.DefaultApiRequestSigner;
import org.jprogger.myworld.api.secure.SignRequestException;
import org.jprogger.myworld.api.service.FriendService;
import org.jprogger.myworld.api.service.FriendServiceImpl;
import org.jprogger.myworld.app.auth.AppUserFactory;
import org.jprogger.myworld.app.auth.model.AppUser;
import org.jprogger.myworld.app.auth.service.AppUserHttpSessionService;
import org.jprogger.myworld.app.auth.service.AuthValidationService;

/**
 * Servlet implementation class SampleApiClientServlet
 */
public class SampleApiClientServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException {
		
		String appIdParam = request.getParameter("app_id");
		String viewerIdParam = request.getParameter("vid");
		String authKeyParam = request.getParameter("authentication_key");
		
		AppUser currentUser = null;
		AppUserFactory userFactory = AppUserFactory.getInstance();
		AuthValidationService authService = new AuthValidationService();
		AppUserHttpSessionService userSessionService = new AppUserHttpSessionService();
		
		boolean isValidAppId = authService.isValidAppId(appIdParam);
		boolean isValidAuthKey = authService.isValidAuthKey(viewerIdParam, authKeyParam);
		
		if (isValidAuthKey && isValidAppId) 
		{		
			currentUser = userFactory.createAppUser(request);
			userSessionService.setAppUserToHttpSession(currentUser, request);
		}
		else 
		{
			response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
		}
		
		try 
		{
			Map<String, Object> params = new HashMap<String, Object>();
			
			params.put("secure", "true");
			params.put("format", "json");
			params.put("method", "friends.get");
			params.put("app_id", currentUser.getApplication().getId());
			params.put("session_key", currentUser.getAuth().getSessionKey());
			
			ApiRequestBuilder builder = new DefaultApiRequestBuilder();
			ApiRequest apiRequest = builder.buildRequest(params);
			
			ApiRequestSigner signer = new DefaultApiRequestSigner();
			apiRequest = signer.signRequest(apiRequest, "your_secret_key");
			
			FriendService friendService = new FriendServiceImpl();
			List<User> users = friendService.getFriends(apiRequest);
			
			PrintWriter writer = response.getWriter();
			writer.write("<html>");
			writer.write("<head>");
			writer.write("</head>");
			writer.write("<body>");
				for (User user : users) {
					writer.write(user.getUseId());
				}
			writer.write("</body>");
			writer.write("</html>");
		} 
		catch (SignRequestException sre) {
			sre.printStackTrace();
		}
		catch (HttpException he) 
		{
			he.printStackTrace();
		}
		catch (ApiException ae) 
		{
			ae.printStackTrace();
		}
		catch (ParseResponseException pre) 
		{
			pre.printStackTrace();
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
